package com.huaxin.device.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.huaxin.device.dto.ResponseResult;
import com.huaxin.device.dto.enums.EquipmentStatus;
import com.huaxin.device.dto.enums.validation.ValidEquipmentStatus;
import com.huaxin.device.dto.request.ElectricalEquipmentCreateReqDTO;
import com.huaxin.device.dto.request.ElectricalEquipmentPageListReqDTO;
import com.huaxin.device.dto.request.ElectricalEquipmentSetMaintenanceReqDTO;
import com.huaxin.device.dto.request.ElectricalEquipmentUpdateReqDTO;
import com.huaxin.device.dto.response.EquipmentStatsRespDTO;
import com.huaxin.device.models.ElectricalEquipment;
import com.huaxin.device.service.ElectricalEquipmentService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "电力设备")
@RestController
@RequiredArgsConstructor
@RequestMapping("/v1/device/electricalEquipment")
public class ElectricalEquipmentController {

    private final ElectricalEquipmentService service;

    @Operation(summary = "创建电力设备", description = "创建电力设备")
    @PostMapping("/create")
    public ResponseResult<ElectricalEquipment> create(@RequestBody @Valid ElectricalEquipmentCreateReqDTO in) {
        return service.create(in);
    }

    @Operation(summary = "获取电力设备", description = "获取电力设备")
    @GetMapping("/get")
    public ResponseResult<ElectricalEquipment> get(
            @Parameter(description = "设备ID", example = "eq123e456-789a-bcde-f012-3456789abcde")
            @RequestParam(value = "id", required = true) String id) {
        ElectricalEquipment entity = service.getByEquipmentId(id);
        return ResponseResult.ok(entity);
    }

    @Operation(summary = "更新电力设备", description = "更新电力设备")
    @PostMapping("/update")
    public ResponseResult<String> update(@RequestBody @Valid ElectricalEquipmentUpdateReqDTO in) {
        return service.update(in);
    }

    @Operation(summary = "删除电力设备", description = "删除电力设备")
    @DeleteMapping("/{id}/delete")
    public ResponseResult<String> delete(
            @Parameter(description = "设备ID", example = "eq123e456-789a-bcde-f012-3456789abcde")
            @PathVariable String id) {
        return service.delete(id);
    }

    @Operation(summary = "切换设备状态", description = "切换设备状态")
    @GetMapping("/switchStatus")
    public ResponseResult<String> switchStatus(
            @Parameter(description = "设备ID", example = "eq123e456-789a-bcde-f012-3456789abcde")
            @RequestParam(value = "id", required = true) String id,
            @Parameter(description = "设备状态：normal/fault/maintenance/repair/offline", example = "normal")
            @RequestParam(value = "status", required = true) @ValidEquipmentStatus String status) {
        return service.switchStatus(id, EquipmentStatus.of(status));
    }

    @Operation(summary = "分页查询电力设备", description = "分页查询电力设备")
    @PostMapping("/page")
    public ResponseResult<IPage<ElectricalEquipment>> pageQuery(@RequestBody @Valid ElectricalEquipmentPageListReqDTO in) {
        return service.pageQuery(in);
    }

    @Operation(summary = "批量设置维保计划")
    @PostMapping("/setMaintenance")
    public ResponseResult<String> batchSetMaintenance(@RequestBody ElectricalEquipmentSetMaintenanceReqDTO in) {
        return service.batchSetMaintenance(in);
    }

    @Operation(summary = "根据设备型号ID查询设备", description = "根据设备型号ID查询设备")
    @GetMapping("/listByModelId")
    public ResponseResult<List<ElectricalEquipment>> listByModelId(
            @Parameter(description = "型号ID", example = "eq123e456-789a-bcde-f012-3456789abcde")
            @RequestParam(value = "modelId", required = true) String modelId) {
        return ResponseResult.ok(service.listByModelId(modelId));
    }

    @Operation(summary = "根据设备类别ID查询设备", description = "根据设备类别ID查询设备")
    @GetMapping("/listByCategoryId")
    public ResponseResult<List<ElectricalEquipment>> listByCategoryId(
            @Parameter(description = "类别ID", example = "eq123e456-789a-bcde-f012-3456789abcde")
            @RequestParam(value = "categoryId", required = true) String categoryId) {
        return ResponseResult.ok(service.listByCategoryId(categoryId));
    }

    @Operation(summary = "根据配电房ID查询设备", description = "根据配电房ID查询设备")
    @GetMapping("/listByRoomId")
    public ResponseResult<List<ElectricalEquipment>> listByRoomId(
            @Parameter(description = "配电房ID", example = "eq123e456-789a-bcde-f012-3456789abcde")
            @RequestParam(value = "roomId", required = true) String roomId) {
        return ResponseResult.ok(service.listByRoomId(roomId));
    }

    @Operation(summary = "根据电井ID查询设备", description = "根据电井ID查询设备")
    @GetMapping("/listByWellId")
    public ResponseResult<List<ElectricalEquipment>> listByWellId(
            @Parameter(description = "电井ID", example = "eq123e456-789a-bcde-f012-3456789abcde")
            @RequestParam(value = "wellId", required = true) String wellId) {
        return ResponseResult.ok(service.listByWellId(wellId));
    }

    @Operation(summary = "根据变电箱ID查询设备", description = "根据变电箱ID查询设备")
    @GetMapping("/listByCabinetId")
    public ResponseResult<List<ElectricalEquipment>> listByCabinetId(
            @Parameter(description = "变电箱ID", example = "eq123e456-789a-bcde-f012-3456789abcde")
            @RequestParam(value = "cabinetId", required = true) String cabinetId) {
        return ResponseResult.ok(service.listByCabinetId(cabinetId));
    }

    @Operation(summary = "获取设备统计信息")
    @GetMapping("/stats")
    public ResponseResult<EquipmentStatsRespDTO> getStats() {
        return service.getStats();
    }
}